import React from 'react';
import type { ConnectProps } from 'umi';
import { Redirect, connect, RoutePermission } from 'umi';
import { getRouteAuthority } from '@/utils/utils';
import type { ConnectState, UserModelState } from '@/models/connect';
import Authorized from '@/components/Authorized/Authorized';
import { baseUrl } from '@/utils/httpUrl';
import { AUTH_TOKEN } from '@/utils/request';
interface AuthComponentProps extends ConnectProps {
  user: UserModelState;
}

const AuthComponent: React.FC<AuthComponentProps> = ({
  children,
  route = {
    routes: [],
  },
  location = {
    pathname: '',
  },
  // user,
}) => {
  // const { currentUser } = user;
  const { routes = [] } = route;
  const isLogin = !!localStorage.getItem(AUTH_TOKEN);
  return (
    <Authorized
      authority={getRouteAuthority(location.pathname, routes) || ''}
      noMatch={isLogin ? <Redirect to="/exception/403" /> : <Route render={() => {
        window.location.href = baseUrl!.login;
        return <Redirect to={baseUrl!.login} />;
      }} />}
    >
      {children}
    </Authorized>
  );
};

export default connect(({ user }: ConnectState) => ({
  user,
}))(AuthComponent);
